from rest_framework.serializers import ModelSerializer
from .models import User


class UserViewSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

    def save(self, **kwargs):
        user = User.objects.create_user(username=self.data['username'], password='123456',
                                        group_name=self.data['group_name'], email=self.data['username'])
        return user


class CommonUserViewSerializer(ModelSerializer):
    class Meta:
        model = User
        fields = ['username', 'email', 'group_name', 'is_staff']

    def update(self, instance, validated_data):
        instance.set_password(validated_data['newPassword'])
        return instance
